package com.lanting.meetingserver.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.util.StringUtils;

import sun.misc.BASE64Decoder;

import com.lanting.constant.Constants;
import com.lanting.meetingserver.domain.ErrorMsg;

public class Common {

	
	public static Map<String,Object> getParamsFromContent(String content){
		String[] params = content.split("&");
		Map<String, Object> map = new HashMap<String, Object>();
		for (String param : params) {
			String[] s = StringUtils.split(param,"=");
			map.put(s[0],s[1]);
		}
		return map;
		
	}
	
	public static String unEncrypt(String str) throws IOException {
		if (str == null || str.equals("")) {
			   return "";
		}
		BASE64Decoder decoder = new BASE64Decoder();
		byte[] b = decoder.decodeBuffer(str);
		return new String(b);
	}

	public static ErrorMsg getErrorMsg(int code,int level,String msg){
		ErrorMsg msg1 = new ErrorMsg();
		msg1.setCode(code);
		
		msg1.setMsg(msg.isEmpty()?ConvertEnum2Str.getErrorMsgByErrorCode(code)
				:msg);
		msg1.setLevel(level);
		return msg1;
	}
	
	public static String getSecurityPassword(String password,String loginId){
		Md5PasswordEncoder encoder = new Md5PasswordEncoder();
		return encoder.encodePassword(password,loginId).toUpperCase();
	}
	
	public static void download(HttpServletRequest request, HttpServletResponse response,String fileName,String filePath) throws IOException{
		response.setContentType("text/html;charset=utf-8");  
        request.setCharacterEncoding("UTF-8");  
        java.io.BufferedInputStream bis = null;  
        java.io.BufferedOutputStream bos = null;  
   
        String downLoadPath = filePath+fileName;
       // System.out.println(downLoadPath);
        try {  
            long fileLength = new File(downLoadPath).length();  
            response.setContentType("application/x-msdownload;"); 
            response.setHeader("Content-disposition", "attachment; filename="  
                    + new String(fileName.getBytes("utf-8"), "ISO8859-1"));  
            response.setHeader("Content-Length", String.valueOf(fileLength));  
            bis = new BufferedInputStream(new FileInputStream(downLoadPath));   
            bos = new BufferedOutputStream(response.getOutputStream());  
            byte[] buff = new byte[2048];  
            int bytesRead;  
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {  
                bos.write(buff, 0, bytesRead);  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            if (bis != null)  
                bis.close();  
            if (bos != null)  
                bos.close();  
        }  
	}
	
	private static String getUploadRootPath(){
		return Constants.ROOT_UPLOAD_PATH;
	}
	
//	@Value("${uploadRootPath}")
//	private static String uplaodRootPath;
	
	
	public static String getUploadPath(int meetingId,int pptId){
		return String.format("%smeeting_%d/ppt_%d/src/",getUploadRootPath(),meetingId,pptId);
	}
	
	public static String getUploadPPTImagePath(int meetingId,int pptId,int pageId){
		return String.format("%smeeting_%d/ppt_%d/src/images/%d/",getUploadRootPath(),meetingId,pptId,pageId);
	}
	
	public static String getUploadPPTImageRootPath(int meetingId,int pptId){
		return String.format("%smeeting_%d/ppt_%d/src/images/",getUploadRootPath(),meetingId,pptId);
	}
	
    public static String getUploadPPTName(int meetingId,int pptId){
		return String.format("%d_%d.ppt",meetingId,pptId);
	}
	public static String getUploadPDFName(int meetingId,int pptId){
		return String.format("%d_%d.pdf",meetingId,pptId);
	}
	

	
	
	
	public static String getPPTFileAllName(int fileType,int meetingId,int pptId,int userId){
		return getPPTFilePath(fileType, meetingId, pptId, userId)+getPPTFileName(meetingId, pptId);
	}
	
	public static String getPPTFilePath(int fileType,int meetingId,int pptId,int userId){
		if (fileType ==CommonEnum.PPT_FILE_SRC) {
			return String.format("%smeeting_%d/ppt_%d/src/",getUploadRootPath(),meetingId,pptId);
		}else {
			return String.format("%smeeting_%d/ppt_%d/bak/user_%d/",getUploadRootPath(),meetingId,pptId,userId);
		}
	}
	
	public static String getPPTFileName(int meetingId,int pptId){
		return String.format("%d_%d.zip",meetingId,pptId);
	}
	
	public static void main(String ...strings) {
		Md5PasswordEncoder encoder = new Md5PasswordEncoder();
		System.out.println(encoder.encodePassword("111111","handy").toUpperCase());
	}
}
